package com.itheimajdbc;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

//用户登录
public class jdbcdemo_UserLogin{

    //演示SQl注入
    @Test
    public void testResultSet() throws Exception{
        //注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接：(如果是本地的mysql并且端口号默认的3306可以简化书写)
        String url = "jdbc:mysql://localhost:3306/mysql";
        String username = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url,username,password);
        //接受用户名和密码('or'1' = '1)
        String name = "zhangsan";
        String pwd = "'or'1' = '1";
        //3.定义sql
        String sql = "select * from password where username ='"+name+"' and password = '"+pwd+"'";
        System.out.println(sql);
        //获取stmt对象
        Statement stmt = conn.createStatement();

        //执行sql
        ResultSet rs = stmt.executeQuery(sql);
        //判断登录是否成功
        if(rs.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }

        //7.释放资源
        rs.close();
        stmt.close();
        conn.close();
    }
}